package com.tju.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.Result;
import com.tju.eduservice.entity.EduTeacher;
import com.tju.eduservice.entity.vo.TeacherQuery;
import com.tju.eduservice.service.EduTeacherService;
import com.tju.serviceHandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author gsc
 * @since 2021-11-25
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;


    //查询讲师列表
    @ApiOperation(value = "查询讲师列表")
    @GetMapping("findAll")
    public Result findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items", list);
    }

    //逻辑删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/deleteTeacherById/{id}")
    public Result deleteTeacherById(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //分页查询讲师
    @PostMapping("pageListTeacher/{page}/{limit}")
    public Result pageList(@PathVariable long page,@PathVariable long limit){
        Page<EduTeacher> page1 = new Page<>(page, limit);
        teacherService.page(page1,null);
        long total = page1.getTotal();//获取总的记录数量
        List<EduTeacher> records = page1.getRecords();//数据list集合
        return Result.ok().data("total",total).data("records",records);
    }

    //实现条件分页查询
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public Result pageListCondition(@PathVariable long page,@PathVariable long limit,
                                    @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> page1 = new Page<>(page,limit);

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询  根据从前端获取的条件来判断拼接出条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.gt("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.lt("gmt_create",end);
        }

        teacherService.page(page1,wrapper);
        long total = page1.getTotal();
        List<EduTeacher> records = page1.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.save(teacher);
        if(flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }

    //根据id进行查询讲师
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);

        if (teacher != null) {
            return Result.ok().data("teacher",teacher);
        }else {
            throw new GuliException(20001,"自定义异常处理");
//            return Result.error();
        }
    }
    //讲师修改功能 先查询然后修改
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher teacher){
        boolean flag = teacherService.updateById(teacher);
        if (flag){
            return Result.ok();
        }else {
            return Result.error();
        }
    }










}

